﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.IO;
using System.Linq;

public partial class Dashboard : System.Web.UI.Page
{
    private Configuration _configuration;

    private class EventLogInfo
    {        
        public int Entries {get;set;}

        public int Errors { get; set; }

        public int Warnings { get; set; }

        public int Infos { get; set; }

        public int Successes { get; set; }

        public int Fails { get; set; }

        public string Name { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {       
        _configuration = Configuration.Instance;        
        LoadServices();
        LoadEventLogs();
        LoadDisks();
        LoadSystemInfo();       
    }   

    private void LoadSystemInfo()
    {
        labOS.Text = Environment.OSVersion.ToString();
        labHost.Text = Environment.MachineName;
        
        TimeSpan ts = TimeSpan.FromTicks((long)Environment.TickCount*10000);        
        labUptime.Text = String.Format("{0} days {1} hours {2} minutes {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);

        labProcesses.Text = (Process.GetProcesses()).Length.ToString();

        cbNetwork.Checked = NetworkInterface.GetIsNetworkAvailable();
    }

    private void LoadDisks()
    {
        _configuration.RefreshDisks();
        lvDisks.DataSource = _configuration.Disks;
        lvDisks.DataBind();
    }

    private void LoadEventLogs()
    {
        _configuration.RefreshEventLogs();
        if (_configuration.ShowEventLogsOnDashboard)
        {            
            EventLogInfo[] infos = new EventLogInfo[_configuration.EventLogs.Length];
            int i = 0;
            foreach (string name in _configuration.EventLogs)
            {
                infos[i] = LoadEventLog(new EventLog(name));
                i++;
            }

            lvEventLogs.DataSource = infos;
            lvEventLogs.DataBind(); 
        }
    }

    private EventLogInfo LoadEventLog(EventLog ev)
    {        
        EventLogInfo info = new EventLogInfo();
        info.Name = ev.LogDisplayName;
        DateTime dt = DateTime.Now.Subtract(new TimeSpan(24, 0, 0));

        var q = from EventLogEntry entry in ev.Entries
                where entry.TimeWritten > dt
                group entry by entry.EntryType into g
                select new {Key=g.Key,Count=g.Count()};
        var dict = q.ToDictionary(i => i.Key);

        return new EventLogInfo()
        {
            Entries = ev.Entries.Count,
            Name = ev.LogDisplayName,
            Errors = dict.ContainsKey(EventLogEntryType.Error) ? dict[EventLogEntryType.Error].Count : 0,
            Fails = dict.ContainsKey(EventLogEntryType.FailureAudit) ? dict[EventLogEntryType.FailureAudit].Count : 0,
            Infos = dict.ContainsKey(EventLogEntryType.Information) ? dict[EventLogEntryType.Information].Count : 0,
            Successes = dict.ContainsKey(EventLogEntryType.SuccessAudit) ? dict[EventLogEntryType.SuccessAudit].Count : 0,
            Warnings = dict.ContainsKey(EventLogEntryType.Warning) ? dict[EventLogEntryType.Warning].Count : 0
        };
    }

    private void LoadServices()
    {
        _configuration.RefreshServices();
        lvServices.DataSource = _configuration.Services;
        lvServices.DataBind();
    }

    protected string GetEventLogStateImage(object dataItem)
    {
        EventLogInfo i = (EventLogInfo)dataItem;
        if (i.Errors > 0)
            return "~/Images/EventLogs/Error.JPG";
        else if(i.Warnings >0)
            return "~/Images/EventLogs/Warning.JPG";
        else
            return "~/Images/EventLogs/Good.JPG";
    }
}
